package com.hugedata.cdn.service.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.bean.TaskInfo;
import com.hugedata.cdn.outerfacade.serviceadapter.soapagent.request.AddTaskRequest;
import com.hugedata.cdn.util.CdnException;
import com.hugedata.cdn.util.ResultCode;

/**
 * 
 * 项目名称：cdn 类名称：SerializeUtil 类描述： 创建人：zhaoaihua 创建时间：2013-11-1 下午7:52:36 修改备注：
 * 
 * @version
 * 
 */
public class SerializeUtil
{

    protected final Log log = LogFactory.getLog(getClass());

    public byte[] serializeObject(Object object) throws CdnException
    {

        byte[] bytes = null;

        ObjectOutputStream oos = null;

        ByteArrayOutputStream saos = null;

        try
        {

            saos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(saos);

            oos.writeObject(object);

            oos.flush();

            bytes = saos.toByteArray();

        }
        catch (IOException e)
        {

            log.error("ret = " + ResultCode.SerializeDataError
                    + "--Serialize  object fail");

            throw new CdnException(ResultCode.SerializeDataError, e);

        }
        finally
        {

            closeStream(saos);
            closeStream(oos);
        }

        return bytes;

    }

    /**
     * 反序列化对象
     * 
     * @throws CdnException
     * 
     * @throws IOException
     * 
     * @throws ClassNotFoundException
     */

    public Object deserializeObject(byte[] buf) throws CdnException
    {

        Object object = null;

        ByteArrayInputStream sais = null;

        ObjectInputStream ois = null;

        try
        {
            sais = new ByteArrayInputStream(buf);
            ois = new ObjectInputStream(sais);

            object = ois.readObject();

        }
        catch (Exception e)
        {

            log.error("ret = " + ResultCode.SerializeDataError
                    + "--Serialize  object fail");

            throw new CdnException(ResultCode.SerializeDataError, e);
        }
        finally
        {

            closeStream(sais);
            closeStream(ois);
        }

        return object;

    }

    /**
     * 关闭流资源
     * 
     * @param closeable
     *            流资源
     */
    private void closeStream(Closeable closeable)
    {
        if (closeable != null)
        {
            try
            {
                closeable.close();
            }
            catch (IOException e)
            {
                log.error("closeStream Failed.");
            }
        }
    }

    public static void main(String[] args)
    {

        AddTaskRequest request = new AddTaskRequest();

        TaskInfo info = new TaskInfo();
        info.setTaskID(1);

        // String[] lis = {
        // "11", "22" };
        // info.setPoints(lis);

        request.setTaskInfo(info);

        SerializeUtil test = new SerializeUtil();

        try
        {
            byte[] bytes = test.serializeObject(request);

            System.out.println(bytes.length);

            AddTaskRequest querst = (AddTaskRequest) test
                    .deserializeObject(bytes);

            System.out.println(querst);

        }
        catch (CdnException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
